package com.yxj.exo;

import static com.yxj.lib_exo.ExoPlayerView.showLog;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.yxj.lib_exo.ExoPlayerControl;
import com.yxj.lib_exo.ExoPlayerManager;
import com.yxj.lib_exo.ExoPlayerView;
import com.yxj.lib_exo.PLListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private ExoPlayerView mExoPlayerView;
    // 加载媒体资源（示例：网络视频）
    String videoUrl = "https://i.zhaojinapp.com/ProductImage/2504/18/2504181114391956.mp4";
    String videoUrl1 = "https://i.zhaojinapp.com/ProductImage/2504/18/2504181113417047.mp4";
    String videoUrl2 = "https://i.zhaojinapp.com/ProductImage/2403/18/2403181341363041.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
    }

    public RecyclerView mRecyclerView;

    private void initList() {
        mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(mRecyclerView);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(videoUrl);
            list.add(videoUrl1);
            list.add(videoUrl2);
        }
        mRecyclerView.setAdapter(new MyAdapter(list));
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        ExoPlayerManager.newInstance().pause();
                        break;

                    case RecyclerView.SCROLL_STATE_IDLE:
                        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                        int firstVisiblePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                        MyAdapter.MyViewHolder viewHolder = (MyAdapter.MyViewHolder) recyclerView.findViewHolderForAdapterPosition(firstVisiblePosition);
                        if (viewHolder != null) {
                            viewHolder.mExoPlayerView.play();
                        }
                        break;
                }
            }
        });

//        mRecyclerView.post(new Runnable() {
//            @Override
//            public void run() {
//                autoPlay(0);
//            }
//        });
    }

    /**
     * 指定某一个自动播放
     *
     * @param i
     */
    public void autoPlay(int i) {
        MyAdapter.MyViewHolder viewHolder = (MyAdapter.MyViewHolder) mRecyclerView.findViewHolderForAdapterPosition(i);
        if (viewHolder != null) {
            viewHolder.mExoPlayerView.play();
        }
    }


    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        private List<String> list;

        public MyAdapter(List<String> list) {
            this.list = list;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_video_layout, null));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            ExoPlayerView exoPlayerView = holder.mExoPlayerView;
            ExoPlayerControl control = new ExoPlayerControl(MainActivity.this);
            control.setSeekBarStyle(ExoPlayerControl.SeekBarStyle.PROGRESS);
            exoPlayerView.bindControl(control);
            exoPlayerView.setDataSource(Uri.parse(list.get(position)));
            exoPlayerView.setAutoPlay(position == 0);
            ExoPlayerManager.newInstance().add(exoPlayerView);
            ViewGroup.LayoutParams l = exoPlayerView.getLayoutParams();
            l.width = getResources().getDisplayMetrics().widthPixels;
            l.height = getResources().getDisplayMetrics().heightPixels;
            exoPlayerView.setLayoutParams(l);

            List<Integer> li = new ArrayList<>();
            for (int i = 0; i < 50 ; i++) {
                li.add(i) ;
            }

            holder.btnPL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.mPLListView.setVisibility(View.VISIBLE);
                }
            });

            holder.mPLListView.setData(R.layout.c_s_pl, li, new PLListView.PlListener<Integer>() {
                @Override
                public void bindView(RecyclerView.ViewHolder holder, Integer item, int position) {
                }

                @Override
                public void dragPlView(float r) {
                    showLog("r="+r);
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public ExoPlayerView mExoPlayerView;
            public PLListView mPLListView;
            public View btnPL;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                mExoPlayerView = itemView.findViewById(R.id.mExoPlayerView);
                mPLListView = itemView.findViewById(R.id.mPLListView);
                btnPL = itemView.findViewById(R.id.btnPL);
            }
        }
    }


    // 生命周期管理：暂停、恢复、释放播放器
    @Override
    protected void onPause() {
        super.onPause();
        ExoPlayerManager.newInstance().pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ExoPlayerManager.newInstance().pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ExoPlayerManager.newInstance().release();
    }
}